#include <boost/algorithm/string.hpp>
#include "HtmlTag.h"

using namespace kc_sf;

HtmlTag::HtmlTag(const std::string &text, HtmlTag::Type _type) :
	ht_type(_type) {

	std::string tmp = boost::trim_left_copy(text);
	boost::trim_right(tmp);

	// <tag_name tag_attr1=val1 ...>
	// nazwa
	unsigned int name_end = tmp.find_first_of(" \t\r\n");
	ht_name = tmp.substr(0, name_end);

	// atrybuty
	ht_attrs = tmp.substr(name_end + 1, tmp.size() - name_end - 1);
	boost::trim(ht_attrs);
}

void HtmlTag::addTextContent(const std::string &txt) {
	(ht_value.empty()) ? ht_value = txt : ht_value += txt;
}

/*
// Szuka atrybutu o nazwie attr
std::string HtmlTag::attribute(const std::string &attr) const {
	std::string ret;

	if (!attr.empty()) {

		unsigned int start;
		StringPatternMatcher matcher(ht_attrs, attr + "=");

		if (matcher.match(start, 0)) {
			start += attr.size() + 1; // ="

			if (start < ht_attrs.size() - 1 && ht_attrs[start] == '\"') {
				++start; // bez "
				unsigned int end = ht_attrs.find_first_of('\"', start);
				if (end < ht_attrs.size()) {
					ret = ht_attrs.substr(start, end - start);
				}
			}
		}
	}
	return ret;
}

//*
 HtmlTag * HtmlTree::sibling() {
 if (act_node != NULL && act_node->parent() != NULL) {
 int index = act_node->parent().findChildIndex(act_node);
 if (index != -1 && act_node->parent().childrenNr() > index)
 return act_node->parent().children[index + 1];
 }
 return NULL;
 }
 */
